<?php

/**
 * This is the model class for table "{{product}}".
 *
 * The followings are the available columns in table '{{product}}':
 * @property integer $id
 * @property string $title
 * @property string $title_url
 * @property string $thumbnail
 * @property string $summary
 * @property string $content
 * @property integer $category_id
 * @property integer $status
 * @property integer $language_id
 * @property integer $meta_desc
 * @property integer $meta_keywords
 * @property string $price
 * @property string $OS
 * @property string $CPU
 * @property string $RAM
 * @property string $ROM
 * @property string $Screen
 * @property string $Warranty
 */
class Product extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Product the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{product}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, title_url, thumbnail, summary, category_id, language_id, meta_desc, meta_keywords, price, OS, CPU, RAM, ROM, Screen, Warranty', 'required'),
            array('category_id, status, language_id, meta_desc, meta_keywords', 'numerical', 'integerOnly' => true),
            array('title, title_url', 'length', 'max' => 200),
            array('thumbnail', 'length', 'max' => 100),
            array('price, OS, CPU, RAM, ROM, Screen, Warranty', 'length', 'max' => 20),
            array('content', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, title_url, thumbnail, summary, content, category_id, status, language_id, meta_desc, meta_keywords, price, OS, CPU, RAM, ROM, Screen, Warranty', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'category' => array(self::BELONGS_TO, 'Category', 'category_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'title' => 'Title',
            'title_url' => 'Title Url',
            'thumbnail' => 'Thumbnail',
            'summary' => 'Summary',
            'content' => 'Content',
            'category_id' => 'Category',
            'status' => 'Status',
            'language_id' => 'Language',
            'meta_desc' => 'Meta Desc',
            'meta_keywords' => 'Meta Keywords',
            'price' => 'Price',
            'OS' => 'Os',
            'CPU' => 'Cpu',
            'RAM' => 'Ram',
            'ROM' => 'Rom',
            'Screen' => 'Screen',
            'Warranty' => 'Warranty',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('title_url', $this->title_url, true);
        $criteria->compare('thumbnail', $this->thumbnail, true);
        $criteria->compare('summary', $this->summary, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('status', $this->status);
        $criteria->compare('language_id', $this->language_id);
        $criteria->compare('meta_desc', $this->meta_desc);
        $criteria->compare('meta_keywords', $this->meta_keywords);
        $criteria->compare('price', $this->price, true);
        $criteria->compare('OS', $this->OS, true);
        $criteria->compare('CPU', $this->CPU, true);
        $criteria->compare('RAM', $this->RAM, true);
        $criteria->compare('ROM', $this->ROM, true);
        $criteria->compare('Screen', $this->Screen, true);
        $criteria->compare('Warranty', $this->Warranty, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getPublicUrl()
    {
        return Yii::app()->createUrl('site/details', array('url' => $this->title_url));
    }

}